<META http-equiv="refresh" content="600" />
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
body{font-size:12px;}
td{font-size:12px;}
</style>
<?php

/**
 * 数据采集
 */
header('Content-type: text/html; charset=utf-8');

include_once 'module/simple_html_dom.php';

$matchId = $_GET["matchId"];
if ($matchId == "") {
	return;
}

//连接数据库
$config = require 'conf/config.ini.php';      //加载公用config.ini.php
$conn = mysqli_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"], $config["DB_NAME"]);

$matchDetail = file_get_contents("http://data.sports.sina.com.cn/livecast/log/log_" . $matchId . ".js?" . rand());
$matchDetail = iconv("GBK", "UTF-8", $matchDetail);
$matchDetail = str_replace("show_log(", "", $matchDetail);
$matchDetail = str_replace("}])", "}]", $matchDetail);
$matchDetail = json_decode($matchDetail);

//先清空当前比赛场次的数据
$sql = "delete from live_score where matchId = '" . $matchId . "';";
$sql .= "INSERT INTO live_score (scoreId, matchId, scoreTime, scoreAction) VALUES";

$i = 0;
$id = 0;
$match_st = null;
$match_m = null;
foreach ($matchDetail as $match) {
	if (isset ($match->m)) {

		if (strpos($match->m, "...") === 0) {
			$match->m = str_replace("...", "", $match->m);
		}
		//不插入第一位是.的内容
		if (strpos($match->m, ".") === 0) {
			continue;
		}
		//不插入:内容
		if (strpos($match->m, ":")) {
			continue;
		}
		//不插入包含图片的内容
		if (strpos($match->m, "img") > 0) {
			continue;
		}

		$match_st = $match->t;
		$match_m = $match->m;

		$id = $i++;
		$sql .= "('" . $i . "', '" . $matchId . "','" . $match_st . "', '" . $match_m . "'),";

	}
}
echo "<table width='100%'>";
echo "<tr>";
echo "<td>" . $id . "&nbsp;&nbsp;&nbsp;&nbsp;" . $match_st . "</td>";
echo "</tr><tr>";
echo "<td>" . $match_m . "</td>";
echo "</tr>";
echo "</table>";

$sql = "SET NAMES utf8;" . substr($sql, 0, strlen($sql) - 1) . ";";
//插入比赛比分
mysqli_multi_query($conn, $sql);

?>